package org.smileface.designpattern.singleton;

/**
 * @description: 单例模式：双重检查（doubleCheck）懒汉式
 * @author: chujian
 * @since: 2022-02-14 09:37
 **/
public class Singleton2 {
    private Singleton2() {
    }

    // doubleCheck 使用 volatile 避免指令重排
    private static volatile Singleton2 INSTANCE = null;

    public static Singleton2 getInstance() {
        // 第一次检查
        if (INSTANCE == null) {
            synchronized (Singleton2.class) {
                // 第二次检查
                if (INSTANCE == null) {
                    INSTANCE = new Singleton2();
                }
            }
        }
        return INSTANCE;
    }
}
